package com.shangcheng.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.AdminMapper;
import com.shangcheng.model.domain.Admin;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

@DubboService
public class AdminApiImpl implements AdminApi {

    @Autowired
    private AdminMapper adminMapper;

    //根据用户名查询用户
    @Override
    public Admin selectOne(String username) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getUsername, username);
        Admin admin = adminMapper.selectOne(lambdaQueryWrapper);
        return admin;
    }

    //新用户注册
    @Override
    public Admin register(Admin admin1) {
        adminMapper.insert(admin1);
        return admin1;
    }

    //删除指定用户信息
    @Override
    public int deleteById(Integer id) {
        return adminMapper.deleteById(id);
    }

    //根据用户名或姓名分页获取用户列表
    @Override
    public Page<Admin> selectPage(Integer pageNum, Integer pageSize, String keyword) {
        Page page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.hasText(keyword),Admin::getUsername,keyword).or()
                .like(StringUtils.hasText(keyword),Admin::getNickName,keyword);




        wrapper.orderByDesc(Admin::getCreateTime);
        //分页
        Page<Admin> pageInfo = adminMapper.selectPage(page, wrapper);
        return pageInfo;
    }

    // 修改指定用户状态
    // 根据id查询用户信息
    @Override
    public Admin selectById(Integer id) {
        Admin admin = adminMapper.selectById(id);
        return admin;
    }

    //修改指定用户的信息
    @Override
    public int updateById(Admin admin) {
        int update = adminMapper.updateById(admin);
        return update;
    }
}
